package com.zjh.document.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zjh.common.entity.Response;
import com.zjh.document.entity.DocumentUserStorageConfig;
import com.zjh.document.service.IDocumentUserStorageConfigService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-06-21
 */
@RestController
@RequestMapping("/documentManager/document-user-storage-config")
public class DocumentUserStorageConfigController {

    @Resource
    private IDocumentUserStorageConfigService documentUserStorageConfigService;

    @GetMapping("getStorageConfig")
    public Response<?> getStorageConfig(@RequestParam("userId") String userId) {
        Long _userId = Long.valueOf(userId);

        QueryWrapper<DocumentUserStorageConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", _userId);
        DocumentUserStorageConfig one = documentUserStorageConfigService.getOne(queryWrapper);
        if (one == null) {
            DocumentUserStorageConfig newConfig = new DocumentUserStorageConfig();
            newConfig.setUserId(_userId);
            newConfig.setStorageType(0); // 默认为Minio存储
            newConfig.setStoragePath("/home");
            documentUserStorageConfigService.save(newConfig);
            return Response.success(newConfig);
        } else {
            return Response.success(one);
        }

    }

    @PostMapping("saveOrUpdate")
    public Response<?> saveOrUpdate(@RequestBody DocumentUserStorageConfig documentUserStorageConfig) {
        QueryWrapper<DocumentUserStorageConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", documentUserStorageConfig.getUserId());
        List<DocumentUserStorageConfig> list = documentUserStorageConfigService.list(queryWrapper);

        if (list.isEmpty()) {
            documentUserStorageConfig.setUserId(documentUserStorageConfig.getUserId());
            documentUserStorageConfigService.save(documentUserStorageConfig);
        } else {
            documentUserStorageConfigService.update(documentUserStorageConfig, queryWrapper);
        }

        return Response.success();
    }
}

